<?php

/**
 * Syslog - Very simple system logger wrapper class
 */
class Syslog {

	protected $facility;
	protected $option;
	protected $ident;
	protected $isOpened = false;

	public function __construct($facility = LOG_LOCAL1, $option = 0, $ident = '[tinymvc]') {
		$this->setup($facility, $option, $ident);
	}

	public function __desctruct() {
		if($this->isOpened) {
			$this->close();
		}
	}

	public function setup($facility, $option, $ident) {
		$this->setFacility($facility);
		$this->setOption($option);
		$this->setIdent($ident);
	}

	public function setFacility($facility) {
		$this->facility = $facility;
		if($this->isOpened) {
			$this->close();
		}
	}

	public function setOption($option) {
		$this->option = $option;
		if($this->isOpened) {
			$this->close();
		}
	}

	public function setIdent($ident) {
		$this->ident = $ident;
		if($this->isOpened) {
			$this->close();
		}
	}

	protected function openlog() {
		openlog($this->ident, $this->option, $this->facility);
		$this->isOpened = true;
	}

	public function send($message, $priority = LOG_INFO) {
		if(!$this->isOpened) {
			$this->openlog();
		}
		syslog($priority, $message);
	}

	public function open() {
		$this->openlog();
	}

	public function close() {
		$this->isOpened = false;
		closelog();
	}
}
